package br.com.ldml2;

import br.com.ldml.Tripla;
import br.com.util.Escritor;
import br.com.util.TripleSet;

import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;

@XStreamAlias("synonym")
public class Synonym implements LdmlCommand {
	private static final String similiarProperty = "http://www.w3.org/2002/07/owl#sameAs";
	
	@XStreamAsAttribute
	private String dataset, property1, property2, output;
	
	@Override
	public void exec(String tmpFileName) {
		TripleSet tsIn = new TripleSet(dataset);
		
		Tripla t = new Tripla(property1, similiarProperty, property2);
		
		for(String subject : tsIn.getTripleSet().keySet()) {
			if (tsIn.getTripleSet().get(subject).containsKey(property1)) {
				tsIn.addTriple(t);
			}
		}
		
		XStream xs2 = new XStream();
		Escritor e = new Escritor();
		e.abreArquivo(tmpFileName, true);
		e.gravaArquivo(new String[] { xs2.toXML(tsIn) } );
		
		e.fechaArquivo();
	}

	@Override
	public String getOutput() {
		return output;
	}

	@Override
	public void setOutput(String output) {
		this.output = output;
	}
}
